﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
' http://dnntweet.codeplex.com
'
' Copyright (c) 2010, Will Strohl
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this 
' software and associated documentation files (the "Software"), to deal in the Software 
' without restriction, including without limitation the rights to use, copy, modify, 
' merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
' permit persons to whom the Software is furnished to do so, subject to the following 
' conditions:
'
' The above copyright notice and this permission notice shall be included in all copies 
' or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
' OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke
Imports System
Imports System.Data
Imports Microsoft.ApplicationBlocks.Data

Namespace WillStrohl.Modules.Twitter

    Public Class SqlDataProvider
        Inherits DataProvider

#Region " Private Members "

        Private Const ProviderType As String = "data"

        Private p_providerConfiguration As Framework.Providers.ProviderConfiguration = Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private p_connectionString As String = String.Empty
        Private p_providerPath As String = String.Empty
        Private p_objectQualifier As String = String.Empty
        Private p_databaseOwner As String = String.Empty

        ' local string values
        Private Const c_ConnectionStringName As String = "connectionStringName"
        Private Const c_ConnectionString As String = "connectionString"
        Private Const c_ProviderPath As String = "providerPath"
        Private Const c_ObjectQualifier As String = "objectQualifier"
        Private Const c_Underscore As String = "_"
        Private Const c_DatabaseOwner As String = "databaseOwner"
        Private Const c_Period As String = "."
        Private Const c_SProc_Prefix As String = "dnntweet_"

        ' sproc names
        Private Const c_AddSearchHistory As String = "AddSearchHistory"
        Private Const c_UpdateSearchHistory As String = "UpdateSearchHistory"
        Private Const c_DoesSearchHistoryExist As String = "DoesSearchHistoryExist"
        Private Const c_DeleteSearchHistory As String = "DeleteSearchHistory"
        Private Const c_GetSearchHistoryByUserId As String = "GetSearchHistoryByUserId"
        Private Const c_GetSearchHistoryByModuleId As String = "GetSearchHistoryByModuleId"
        Private Const c_GetSearchHistoryBySearchId As String = "GetSearchHistoryBySearchId"
        Private Const c_GetSearchHistory As String = "GetSearchHistory"
        Private Const c_GetSearchHistoryByModuleIdLike As String = "GetSearchHistoryByModuleIdLike"

#End Region

#Region " Constructors "

        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Framework.Providers.Provider = CType(p_providerConfiguration.Providers(p_providerConfiguration.DefaultProvider), Framework.Providers.Provider)

            ' Read the attributes for this provider
            If Not String.IsNullOrEmpty(objProvider.Attributes(c_ConnectionStringName)) AndAlso _
                Not String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings.Item(objProvider.Attributes(c_ConnectionStringName))) Then
                p_connectionString = System.Configuration.ConfigurationManager.AppSettings.Item(objProvider.Attributes(c_ConnectionStringName))
            Else
                p_connectionString = objProvider.Attributes(c_ConnectionString)
            End If

            p_providerPath = objProvider.Attributes(c_ProviderPath)

            p_objectQualifier = objProvider.Attributes(c_ObjectQualifier)
            If Not String.IsNullOrEmpty(p_objectQualifier) And p_objectQualifier.EndsWith(c_Underscore) = False Then
                p_objectQualifier = String.Concat(p_objectQualifier, c_Underscore)
            End If

            ' Add willstrohl_ to the beginning of the sprocs
            p_objectQualifier = String.Concat(p_objectQualifier, c_SProc_Prefix)

            p_databaseOwner = objProvider.Attributes(c_DatabaseOwner)
            If Not String.IsNullOrEmpty(p_databaseOwner) And p_databaseOwner.EndsWith(c_Period) = False Then
                p_databaseOwner = String.Concat(p_databaseOwner, c_Period)
            End If

        End Sub

#End Region

#Region " Properties "

        Public ReadOnly Property ConnectionString() As String
            Get
                Return p_connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return p_providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return p_objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return p_databaseOwner
            End Get
        End Property

#End Region

#Region " Data Provider Implementation "

        Public Overrides Function AddSearchHistory(ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal SearchText As String) As Integer
            Return CType(SqlHelper.ExecuteScalar(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_AddSearchHistory), ModuleId, UserId, SearchText), Integer)
        End Function

        Public Overrides Sub DeleteSearchHistory(ByVal SearchId As Integer)
            SqlHelper.ExecuteNonQuery(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_DeleteSearchHistory), SearchId)
        End Sub

        Public Overrides Function DoesSearchHistoryExist(ByVal UserId As Integer, ByVal SearchText As String) As Boolean
            Return CType(SqlHelper.ExecuteScalar(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_DoesSearchHistoryExist), UserId, SearchText), Boolean)
        End Function

        Public Overrides Function GetSearchHistory() As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_GetSearchHistory)), IDataReader)
        End Function

        Public Overrides Function GetSearchHistoryByModuleId(ByVal ModuleId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_GetSearchHistoryByModuleId), ModuleId), IDataReader)
        End Function

        Public Overrides Function GetSearchHistoryBySearchId(ByVal SearchId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_GetSearchHistoryBySearchId), SearchId), IDataReader)
        End Function

        Public Overrides Function GetSearchHistoryByUserId(ByVal UserId As Integer) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_GetSearchHistoryByUserId), UserId), IDataReader)
        End Function

        Public Overrides Sub UpdateSearchHistory(ByVal SearchId As Integer, ByVal ModuleId As Integer, ByVal UserId As Integer, ByVal SearchText As String)
            SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_UpdateSearchHistory), SearchId, ModuleId, UserId, SearchText)
        End Sub

        Public Overrides Function GetSearchHistoryByModuleIdLike(ByVal ModuleId As Integer, ByVal SearchText As String) As System.Data.IDataReader
            Return CType(SqlHelper.ExecuteReader(Me.ConnectionString, String.Concat(Me.DatabaseOwner, Me.ObjectQualifier, c_GetSearchHistoryByModuleIdLike), ModuleId, SearchText), IDataReader)
        End Function

#End Region

#Region " Private Helper Methods "

        Private Function GetNull(ByVal Field As Object) As Object
            Return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value)
        End Function

#End Region

    End Class

End Namespace